package com.gaeainfo.modules.alarm.collection.dao;

import com.gaeainfo.modules.alarm.collection.entity.AlarmPersonEntity;
import com.gaeainfo.modules.alarm.collection.entity.AlarmVehicleEntity;
import com.gaeainfo.modules.alarm.collection.entity.PersonEntity;
import com.gaeainfo.modules.alarm.collection.entity.VehicleEntity;
import com.gaeainfo.modules.base.dao.BaseHibernateDao;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by Caven on 2018/7/18.
 */
@Repository("alarmCollectionDao")
public class AlarmCollectionDaoImpl extends BaseHibernateDao implements AlarmCollectionDao {
    @Override
    public PersonEntity findPersonByIdCard(String idCard) {
        DetachedCriteria criteria = DetachedCriteria.forClass(PersonEntity.class);
        criteria.add(Restrictions.eq("idCard", idCard));
        List<PersonEntity> list = this.findByCriteria(criteria);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public PersonEntity findPersonByPhone(String phone) {
        DetachedCriteria criteria = DetachedCriteria.forClass(PersonEntity.class);
        criteria.add(Restrictions.eq("phone", phone));
        List<PersonEntity> list = this.findByCriteria(criteria);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void savePerson(PersonEntity personEntity) {
        this.save(personEntity);
    }

    @Override
    public void updatePerson(PersonEntity personEntity) {
        this.update(personEntity);

    }

    @Override
    public void saveAlarmPerson(AlarmPersonEntity alarmPersonEntity) {
        this.save(alarmPersonEntity);

    }

    @Override
    public List<Map<String, Object>> findAlarmPersonByAlarmId(String alarmId) {
        return null;
    }

    @Override
    public AlarmPersonEntity findAlarmPersonById(String alarmId, String personId) {
        DetachedCriteria criteria = DetachedCriteria.forClass(AlarmPersonEntity.class);
        criteria.add(Restrictions.eq("alarmId", alarmId));
        criteria.add(Restrictions.eq("personId", personId));
        List<AlarmPersonEntity> list = this.findByCriteria(criteria);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void deleteAlarmPerson(AlarmPersonEntity alarmPersonEntity) {
        this.delete(alarmPersonEntity);
    }

    @Override
    public VehicleEntity findVehicleByPlateNo(String plateNo) {
        DetachedCriteria criteria = DetachedCriteria.forClass(VehicleEntity.class);
        criteria.add(Restrictions.eq("plateNo", plateNo));
        List<VehicleEntity> list = this.findByCriteria(criteria);
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void saveVehicle(VehicleEntity vehicleEntity) {
        this.save(vehicleEntity);
    }

    @Override
    public void saveAlarmVehicle(AlarmVehicleEntity alarmVehicleEntity) {
        this.save(alarmVehicleEntity);
    }

    @Override
    public AlarmVehicleEntity findAlarmVehicleById(String alarmId, String vehicleId) {
        return null;
    }

    @Override
    public void deleteAlarmVehicle(AlarmVehicleEntity alarmVehicleEntity) {
        this.delete(alarmVehicleEntity);
    }
}
